package com.hospital.wx.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.hospital.wx.dao.PatientUserInfoDao;
import com.hospital.wx.pojo.PatientUserInfo;
import com.hospital.wx.service.PatientUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

@Service
public class PatientUserInfoServiceImpl implements PatientUserInfoService {
    @Autowired
    private PatientUserInfoDao patientUserInfoDao;

    @Override
    @Transactional
    public int insert(PatientUserInfo patientUserInfo) {
        return patientUserInfoDao.insert(patientUserInfo);
    }

    @Override
    public HashMap selectPatientUserInfoByUserId(int userId) {
        HashMap map = patientUserInfoDao.selectPatientUserInfoByUserId(userId);
        JSONArray jsonArray = JSONUtil.parseArray(MapUtil.getStr(map, "medicalHistory"));
        map.replace("medicalHistory",jsonArray);

        return map;
    }

    @Override
    @Transactional
    public void update(PatientUserInfo entity) {
        patientUserInfoDao.update(entity);
    }
}
